import { FC, useState, useEffect } from "react";
import { AformConfig, tableColumns } from "./config/index"
import AForm from "../../compent/AForm";
import { Visits } from "../../untils/api"
import ATable from "../../compent/ABtate";
import { message } from "antd";
const Visit: FC<{}> = () => {
    const [pageSizes, setPageSize] = useState(10)
    const [page, setPage] = useState(1)
    const [total, settotal] = useState(0)
    const [data, setData] = useState([])
    const [fromobj, setFeomObj] = useState<object>({})

    useEffect(() => {
        getList(page)
    }, [])
    const getList = (page: number, pageSize: number = pageSizes, Fromobj: object = fromobj) => {
        const params = {
            page,
            pageSize: pageSize,
            ...Fromobj
        }
        console.log(params)
        Visits.getVisitList(params).then(res => {
            console.log(res.data)
            console.log(res.data.data[0])
            //列表
            setData(res.data.data[0])
            //总数据
            settotal(res.data.data[1])
        })
    }

    const onPageSize = (pageNumber: number, pageSize: number) => {
        console.log(pageNumber, pageSize, "从子组件获取")
        setPage(pageNumber)
        setPageSize(pageSize)
        getList(pageNumber, pageSize)
    }
    //提交表单信息
    const submit = (data: any) => {
        console.log(data)
        setFeomObj(data)
        //调用接口
        getList(page, pageSizes, data)

    }
    //重置
    const onResetFields = () => {
        // setFeomObj({}),
        getList(page, pageSizes, {})
    }
    //删除
    const Del = (id: any) => {
        console.log(id)
        Visits.delVisitList(id).then(res=>{
            if(res.data.statusCode===200){
                message.success("删除成功")
            }
            getList(page)
        })
       
    }

    return (
        <div>
            <AForm
                AformConfig={AformConfig}
                onsubmit={submit}
                onResetFields={onResetFields}
            ></AForm>
            <ATable
                total={total}
                page={page}
                pageSize={pageSizes}
                onchange={onPageSize}
                columns={tableColumns(Del)}
                data={data}></ATable>
        </div>
    )
}

export default Visit